package com.kevin.design.creationalPattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Description jdk 代理
 * @Author Kevin
 * @Date: Created in  11:08 2019/11/6:
 * @Modified By:
 * @Email: tang.lx@topcheer.com
 */
public class ProxyTest {
    public static void main(String[] args) {
        RealI realI = (RealI) Proxy.newProxyInstance(RealI.class.getClassLoader(),new Class[] {RealI.class},new RealHandler());
        realI.print();
    }
}

interface RealI {
    void print();
}

class RealOBJ implements RealI {

    @Override
    public void print() {
        System.out.println("middle");
    }
}

class RealHandler implements InvocationHandler {

    RealI realI;

    public RealHandler() {
        realI = new RealOBJ();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");
        method.invoke(realI,args);
        System.out.println("after");
        return null;
    }
}